<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/view/include/css-include.jsp"%>
<%@ include file="/view/include/jsp-include.jsp"%>
<%@ include file="/view/include/jquery-include.jsp"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>TMI</title>

<script type="text/javascript">
<%--============================= 전역변수 선언 ==========================--%>

//행추가
<%-- <tr id="dynamicDetailAddRow"> 에서 dynamicDetail 까지 중요함..조회, 초기화 시킬때 dynamicDetail으로 시작하는 ID 값 삭제 해줌 
(행추가와, 조회 후 한방에 처리할려면 ID값 다쓰면 귀찮아짐)

ex:
- 행추가는 dynamicDetailAddRow
- 조회 후 생성되는 ROW는dynamicDetailSearchRow 
등으로   dynamicDetail로 조회, 초기화시 한방에 지울 수 있게하자..
--%>
/* outDetailTable에 view화면 호출, 행추가, 초기화 시 필요     */
 /* <th width="10%">OUT DATE</th>
 <th width="10%" align="Center">END USER</th>
 <th width="15%">END USER NAME</th>
 <th width="10%">VIHICLE NO</th>
 <th width="10%">GOODS CODE</th>
 <th width="10%">GOODS NAME</th>
 <th width="10%">LOT.NO</th>
 <th width="10%">OUT QTY</th>
 <th width="5%">D</th>
 +"<td align='center'><a href='javascript:onDel("+this.custCode+");'><img src='images/web/ic_x.gif' alt='Delete' id='onDelete'/></a></td>"
 `OUT_NO`
 `OUT_SEQ`
 `OUT_DATE`
 `OUT_ENDUSER`
 `VIHICLE_NO`
 `GOODS_CODE`
 `MAWB`
 `HAWB`
 `BATCH_NO`
 `QTY`
 readonly onFocus='blur();
 */
var popType ="";
    /************************************************************************/
    /*                          Onload function                             */
    /************************************************************************/

    $().ready(function() {

        <%-- 달력 --%>
        calendar.makeCalender("outDate");
        calendar.makeCalender("receiptDate");
        
        if($("#roleId").val() == "3"){
            $("#onDeliveryNo").attr("style", "display:none");
        }
        
        var keyparam = '${outInvoice}';
        var keyparam2 = '${outNo}';
        if(keyparam != null && keyparam != ""){
        	stockOutNoPopValue(keyparam2, keyparam);
        }     
        <%-- 버튼설정 --%>
        /* 초기화버튼 */
        $("#reSetBtn").bind("click",function(){
              $("form[name='outFrm']").each(function(){
                    this.reset();
              });
              
              //reSetValidete(); //벨류데이션 체크 초기화;
              $("#saveStatus").val("SAVE");     //신규 입력 상태로 변경
              $("table[id='outDetailTable']").empty(); 
              $("#receiptDateSpan").empty();
              $("#receiptYnSpan").empty();
              $("#returnNameSpan").empty();
        });        
        
    });
    
    /************************************************************************/
    /*                          조회                                        */
    /************************************************************************/
    /* 조회 펑션 */
    function view(outInvoice){
        $.post(
                '/getDeliveryView.json', {
                outInvoice : outInvoice,
                outNo : $("#outNo").val()
                },
            function(data) {
                searchDataMaping(data);    //DATA맵핑 
            });
    }


    /************************************************************************/
    /*                          Grid Row Update                             */
    /************************************************************************/
    /* detail update*/
    function modifyDeliveryDetail(){
        if(confirm("Update?")){
            $.post("/modifyDeliveryDetail.json",{
                outNo      : $("#pop_outNo").val(),
                outSeq     : $("#pop_outSeq").val(),
                receiverName  : $("#receiverName").val(),
                receiptYn  : $("#receiptYn").val(),
                receiptDate  : $("#receiptDate").val(),
                vihicleNo  : $("#vihicleNo").val(),
                cause  : $("#cause").val()
            },function(data){
                if(data.result == true){
                    $("#viewDialog").dialog("close");
                    view($("#outInvoice").val());
                }
            });
        }
    }

    /************************************************************************/
    /*                          조회된 DATA MAPPING                         */
    /************************************************************************/
    /* searchData 맵핑 */
    function searchDataMaping(data){
        var val = ""; 
       // for(var v in data.resultMap) {       //상단 조회 결과 맵핑 조회return값이 한건임
            //$("#"+v).val(data.resultMap[v]);
        	$("#sCustCode").val(data.resultMap[0].custCode);
            $("#sCustName").val(data.resultMap[0].custName);
            $("#sAddr1").val(data.resultMap[0].addr1);
            $("#sAddr2").val(data.resultMap[0].addr2);
            $("#sTel").val(data.resultMap[0].tel);
            $("#sFax").val(data.resultMap[0].fax);
            $("#remark").val(data.resultMap[0].remark);
            $("#outInvoice").val(data.resultMap[0].outInvoice);
            $("#outNo").val(data.resultMap[0].outNo);
            $("#transNo").val(data.resultMap[0].transNo);
            $("#requestDate").val(data.resultMap[0].requestDate);
       // }
        
        //하단 그리드 맵핑
        var contents = "";
        var tdTmp = "";
        $.each(data.resultDetailList, function(index){  // List로 받아온값 (여러건)
        	if($("#roleId").val() == "3"){
                tdTmp = "";
            }else{
                tdTmp = '<img src="images/web/ic_up.gif" alt="Update" id="onUpdate" onclick="viewDialogOpen('+this.outNo+' , \''+this.outSeq+'\',\''+this.receiverName+'\',\''+this.receiptDate+'\',\''+this.receiptYn+'\',\''+this.returnCode+'\',\''+this.vihicleNo+'\');" />';
            }
            //(Detail 테이블에 조회결과 입히기 jquery말고 그냥 onclick 걸어줌.. (조회 후 del 번튼에 key값 param변수로 입력)
            var rowNum = index+1;
            contents += '<tr id="dynamicDetailSearchRow'+rowNum+'" onclick="rowSelect(this,\''+this.receiptDate+'\',\''+this.receiptYn+'\',\''+this.returnName+'\');">'   
                +'<td align="center" width="5%">'+rowNum+'</td>'
                +'<td align="center" width="10%">'+this.outDate+'</td>'
                +'<td align="left"   width="17%">'+this.outEndUserName+'</td>'
                +'<td align="center" width="10%">'+this.vihicleNo+'</td>'
                +'<td align="center"  width="18%">'+this.goodsName+'</td>'  
                +'<td align="center"  width="10%">'+this.batchNo+'</td>' 
                +'<td align="right"   width="10%">'+this.qty+'</td>' 
                +'<td align="center"   width="15%">'+this.receiverName+'</td>' 
                +'<td align="center"  width="7%">' 
                +tdTmp
                +'</td>'   //onclick= 삭제 펑션 호출 ( 키값, 해당ROW값)
                +"</tr>";     
        });
        $("table[id='outDetailTable']").empty().append(contents);  // 조회 값 뿌리기
        $("table[id='outDetailTable'] tr:eq(0)").click();
        $("#saveStatus").val("UPDATE"); // 상태값 UPDATE로 변경
        
    }
    
    function rowSelect(obj, receiptDate, receiptYn, returnName){
        /*$("#goodsCodeTmp").val(goodsCode);
        $("#goodsNameTmp").val(goodsName);
        $("#outDateTmp").val(outDate);
        $("#mawbTmp").val(mawb);
        $("#hawbTmp").val(hawb);
        $("#batchNoTmp").val(batchNo);
        $("#stockOutQtyTmp").val(stockOutQty);*/
        
        $("#outDetailTable tr").css("backgroundColor","#FFFFFF");
        obj.style.backgroundColor="#e0eeee";  
        $("#receiptDateSpan").empty().html(receiptDate);
        $("#receiptYnSpan").empty().html(receiptYn);
        $("#returnNameSpan").empty().html(returnName);
    }
       
    /************************************************************************/
    /*                          Dialog                                      */
    /************************************************************************/
    function viewDialogOpen(outNo, outSeq, receiverName, receiptDate, receiptYn, returnCode, vihicleNo){
        $("#pop_outNo").val(outNo);
        $("#pop_outSeq").val(outSeq);
        $("#receiverName").val(receiverName);
        $("#receiptDate").val(receiptDate);
        $("#vihicleNo").val(vihicleNo);
        $("#receiptYn").val(receiptYn);
        if(returnCode == null && returnCode ==""){
        	$("#cause").val("");
        }else{
        	$("#cause").val(returnCode);
        }
        
        
        $("#viewDialog").dialog({
            modal:true, 
            width:300,
            title:"Detail Update"
        });
    }   

    
    function onDeliveryNoPop(){
    	window.open('/stockOutNoPop.html', 'stockOutNoPop', 'width=620, height=540, toolbar=no, location=no, status=no, menubar=no, scrollbars=no');
    }
    
    function stockOutNoPopValue(outNo, outInvoice){
    	$("#outNo").val(outNo);
        $("#outInvoice").val(outInvoice);
        view($("#outInvoice").val());
    }
    /************************************************************************/
    /*                  출고상품 팝업 버튼 , 리턴값처리                     */
    /************************************************************************/
     /* 상품 팝업 버튼 */
    var productIndex = 0;
    function onDeliveryPopup(obj){
        productIndex = $("img[id='stockOutPop']").index(obj);
        window.open('/stockOutPop.html', 'stockOutPop', 'width=650, height=520, toolbar=no, location=no, status=no, menubar=no, scrollbars=no');
    }
    /* 상품정보 맵핑 */
    function stockOutPopValue(goodsCode, goodsName, inDate, mawb, hawb, batchNo, stockQty, outQty){

        $("[name=goodsCode]").eq(productIndex).val(goodsCode);
        $("[name=goodsName]").eq(productIndex).val(goodsName);
        $("[name=batchNo]").eq(productIndex).val(batchNo);
        $("[name=mawb]").eq(productIndex).val(mawb);
        $("[name=hawb]").eq(productIndex).val(hawb);
        $("[name=qty]").eq(productIndex).val(outQty);
    }

</script>
</head>


<body>
<div class="wrap">
    <div class="pagetitle">
        <h1>DELIVERY TO END USER</h1>
    </div>
    <div class="">
    <!-- 레이어 팝업시작 -->
        <div id="viewDialog" style="display: none; background: white;">
         <input type="hidden" id="pop_odrDetailSeq">
           <table class="TableST">
              <tr>
               <th width="30%">Goods receipt</th>
               <td>
                    <select name="receiptYn" id="receiptYn" style="width:120px">
                        <option value="Y">Y</option>
                        <option value="N">N</option>          
                    </select>
               </td>
              </tr>
              <tr>
               <th>Goods receipt date</th>
               <td><input type="text" name="receiptDate" id="receiptDate" class="inSTR80" maxlength="20" readonly/>
               </td>
              </tr>
              <tr>
               <th>Receiver Name</th>
               <td><input type="text" name="receiverName" id="receiverName" class="inST80" maxlength="20"/>
               <input type="hidden" name="pop_outNo" id="pop_outNo"/>
               <input type="hidden" name="pop_outSeq" id="pop_outSeq"/>
               </td>
              </tr>
              <tr>
               <th>Vihicle No</th>
               <td><input type="text" name="vihicleNo" id="vihicleNo" class="inST80" maxlength="20"/>
               </td>
              </tr>
              <tr>
              <th>Cause</th>
              <td>
                <select name="cause" id="cause" style="width:120px">
                    <option value="">select</option>
                    <c:forEach items="${requestScope.listReturnCause}" var="listReturnCauseList">
                    <option value="${listReturnCauseList.returnCode}">${listReturnCauseList.returnName }</option>
                  </c:forEach>
                </select>
              </td>
            </tr>
           </table>
           <div class="bo_btn">
                <ul class="btnst"><li class="a"><a href="javascript:modifyDeliveryDetail();" id="save" >SAVE</a></li></ul>
           </div>  
       </div>
    <!-- 레이어 팝업끝 -->   
       
    <form name="outFrm" method="post" action="/saveDelivery.json"> <%--리턴값 json으로 설정 --%>
    <input type="hidden" id ="saveStatus" name="saveStatus" value="SAVE" />     <%-- 조회, 수정, 초기화, 최초 입력 시 필요 (최초, 초기화 후 = SAVE   ,  수정, 조회 후 = UPDATE)--%>
    
        <table class="TableST">
            <tr>
                <th>Stock Out No</th>
                <td colspan="3">
                    <input type="text" class="inSTR20" id="outInvoice" name="outInvoice" maxlength="5" value="" readonly/>
                    <input type="hidden" id="outNo" name="outNo"/>
                    <img alt="DeliverySearch" height="16" src="/images/web/bkn_zoom.gif" align="absmiddle" id="onDeliveryNo" onClick="onDeliveryNoPop();"></td>
            </tr>
            <tr>
                <th rowspan="2">Messrs</th>
                <td colspan="2">
                    <input type="text" style="margin-bottom:3px;margin-top:3px;" class="inSTR20" id="sCustCode" name="sCustCode" readonly onFocus="blur();" value=""/>
                    <input type="text" style="margin-bottom:3px;margin-top:3px;" class="inSTR70" id="sCustName" name="sCustName" readonly onFocus="blur();" value=""/></td>
                <td><input type="text" style="margin-bottom:3px;margin-top:3px;" class="inSTR90" id="sAddr1" name="sAddr1" readonly onFocus="blur();" value=""/></td>
            </tr>
            <tr>
                <td style="width: 17%"><input type="text" style="margin-bottom:3px;margin-top:3px;" class="inSTR90"id="sTel" name="sTel" readonly onFocus="blur();" value=""/></td>
                <td style="width: 17%"><input type="text" style="margin-bottom:3px;margin-top:3px;" class="inSTR90" id="sFax" name="sFax" readonly onFocus="blur();" value=""/></td>
                <td><input type="text" style="margin-bottom:3px;margin-top:3px;" class="inSTR90" id="sAddr2" name="sAddr2" readonly onFocus="blur();" value=""/></td>
            </tr>
            <tr>
                <th>Local B/L Transportation No</th>
                <td colspan="3">
                    <input type="text" class="inSTR50" id="transNo" name="transNo" maxlength="20" value="" readonly/>
                </td>
            </tr>
            <tr>
                <th>Request Date</th>
                <td colspan="3">
                    <input type="text" class="inSTR20" id="requestDate" name="requestDate" readonly/>
                </td>
            </tr>
            <tr>
                <th>Remarks</th>
                <td colspan="3"><textarea class="teST80" wrap="virtual" id="remark" name="remark" style="background:#eeeeee" readonly></textarea></td>
            </tr>
        </table>
        <br />
        <div class="taHeadW">
            <table class="TableST2Head" >
                <tr>
                    <th width="5%">No</th>
                    <th width="10%">Out Date</th>
                    <th width="17%">End User Name</th>
                    <th width="10%">Vihicle No</th>
                    <th width="18%">Goods Name</th>
                    <th width="10%">Batch</th>
                    <th width="10%">Out Qty</th>
                    <th width="15%">Receiver</th>
                    <th width="7%">U</th>
                </tr>
            </table>
        </div>
        <div class="taBodyW2">
            <table class="TableST2Body" id="outDetailTable">
            </table>
        </div>    
        <br />
    </div>
    <table class="TableST5">
        <tr>
            <th width="15%">Goods receipt</th>
            <td width="10%">
                <span id="receiptYnSpan"></span>
            </td>
            <th width="15%">Goods receipt date</th>
            <td width="10%"><span id="receiptDateSpan"></span>
            </td>
            <th width="15%">Receipt Cause</th>
            <td width="35%"><span id="returnNameSpan"></span>
            </td>
        </tr>       
    </table>
    <div class="bo_btn">
        <ul class="btnst"><li class="a"><a href="#" id="reSetBtn" >INIT</a></li></ul>
    </div>
    <input type="hidden" name="roleId" id="roleId" value="${requestScope.roleId}">
    </form>
</div>
</body>
</html>